package com.itheima.advantagesup.controller;

import com.itheima.advantagesup.common.R;
import com.itheima.advantagesup.dto.RoleDTO;
import com.itheima.advantagesup.entity.SysRole;
import com.itheima.advantagesup.service.SysRoleService;
import com.itheima.advantagesup.vo.PageBean;
import com.itheima.advantagesup.vo.RoleAllDataVO;
import com.itheima.advantagesup.vo.RoleInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/sys_role")
@Slf4j
public class RoleController {

    private final SysRoleService sysRoleService;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param name
     * @return {@link R}<{@link PageBean}>
     */
    @GetMapping("/list")
    public R<PageBean> pageSelect(Integer page,Integer limit,String name){
        PageBean pageBean = sysRoleService.pageSelect(page, limit, name);
        return R.success(pageBean);
    }

    /**
     * 角色列表-全部
     * @return {@link R}
     */
    @GetMapping("/allData")
    public R<List<RoleAllDataVO>> allData(){
        List<RoleAllDataVO> roleAllDataVOList = sysRoleService.allData();
        return R.success(roleAllDataVOList);
    }

    /**
     * 添加角色
     * @param roleDTO
     * @return {@link R}
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody RoleDTO roleDTO){
        sysRoleService.add(roleDTO);
        return R.success();
    }

    /**
     * 根据角色id回显数据
     * @param id
     * @return {@link R}
     */
    @GetMapping("/rowInfo")
    public R<RoleInfoVO> rowInfo(String id){
        RoleInfoVO roleInfoVO = sysRoleService.rowInfo(id);
        return R.success(roleInfoVO);
    }


    /**
     * 修改角色
     * @param roleDTO
     * @return {@link R}
     */
    @PostMapping("/update")
    public R update(@RequestBody RoleDTO roleDTO){
        sysRoleService.update(roleDTO);
        return R.success();
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R deleteById(String id){
        sysRoleService.deleteById(id);
        return R.success();
    }


}
